package org.csu.mypetstore.Controller;
import com.zhenzi.sms.ZhenziSmsClient;
import org.csu.mypetstore.checkcode.MailService;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.LatestProduct;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CategortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.mail.Multipart;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;


@Controller
@RequestMapping("/Main")
public class MainController {

    @Autowired
    CategortService categortService;
    @Autowired
    MailService mailService;
    @GetMapping("/enter")
    public String EntreMain(HttpServletRequest request, Model model) {
        HttpSession session = request.getSession();
        Account account = (Account)session.getAttribute("account");
        List<Category>categoryList =  categortService.getCategoryList();
        List<Product>productList = categortService.getallproduct();
        List<LatestProduct> latestProducts = categortService.getlatestproduct();
        model.addAttribute("account",account);
        model.addAttribute("latestProducts",latestProducts);
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("productList",productList);
        session.setAttribute("latestProducts",latestProducts);
        session.setAttribute("categoryList",categoryList);
        session.setAttribute("productList",productList);
        return "catalog/test";
    }
    @RequestMapping("/JPetStoreDemo")
    public String JPetStoreDemo() {
        return "common/JPetStoreDemo";
    }
    @RequestMapping("/Manage")
    public String Manage() {
        return "Manager/loginin";
    }
    @RequestMapping("/sendSms")
    @ResponseBody
    public void sendSms(HttpServletRequest request) {
        ZhenziSmsClient client = new ZhenziSmsClient("https://sms_developer.zhenzikj.com", "108513", "fdf31ab6-18e0-408b-9e5b-f4178a2ed9a3");
        Map<String, Object> params = new HashMap<String, Object>();
        String phonenumber = request.getParameter("phonenumber");
        String verifyCode = String.valueOf(new Random().nextInt(899999) + 100000);
        params.put("number", phonenumber);
        params.put("templateId", "4318");
        String[] templateParams = new String[2];
        templateParams[0] = verifyCode;
        request.getSession().setAttribute("verifyCode", verifyCode);
        templateParams[1] = "5分钟";
        params.put("templateParams", templateParams);
        try {
            String result = client.send(params);
            System.out.println(result);
        }catch (Exception e){

        }


    }
    @RequestMapping("/viewsendmail")
    public String viewsendmail(){
        return "/common/mailManage";
    }
    @RequestMapping("/sendmail")
    public String sendmail(HttpServletRequest request){
        String content = request.getParameter("textcontent");
        String to  = request.getParameter("to");
        String subject = request.getParameter("subject");
        mailService.sendMail(to,subject,content);
        return "/common/mailManage";
    }


}
